<template>
  <div class="login_container">
    <div class="login_box">
      <el-form :rules="rules" ref="loginForm" :model="loginForm" label-width="80px" class="login_form">
        <h3 class="loginTitle">系统登录</h3>
        <el-form-item label="用户名" prop="username" class="login_label">
          <el-input type="text" auto-complete="false" v-model="loginForm.username" style="width:80%" placeholder="请输入用户名">
            <i slot="prefix" class="iconfont icon-user login_ico"></i>
          </el-input>
        </el-form-item>

        <el-form-item label="密　码" prop="password" class="login_label">
          <el-input type="password" auto-complete="false" v-model="loginForm.password" style="width:80%" placeholder="请输入密码">
            <i slot="prefix" class="iconfont icon-3702mima login_ico"></i>
          </el-input>
        </el-form-item>

        <el-form-item label="验证码" prop="code" class="login_label">
          <el-input v-model="loginForm.code" auto-complete="false" style="width:180px" placeholder="点击图片更换验证码"></el-input>
          <!-- <img :src="captchaUrl" @click="updateCaptcha" /> -->
        </el-form-item>
        <div class="text">
          <el-checkbox v-model="checked" class="loginRemeber">记住我</el-checkbox>
          <a href="#" class="loginLpsd">忘记密码？</a>
          <el-button type="primary" class="login_btn" @click="submitLogin">登录</el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    return {
      // captchaUrl: '/captcha?time=' + new Date(),
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      checked: true,
      rules: {
        username: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
        code: [{ required: true, message: '请输入验证码', trigger: 'blur' }]
      }
    }
  },
  methods: {
    submitLogin() {
      this.$refs.loginForm.validate(async valid => {
        if (valid) {
          const { data: res } = await this.$http.post('login', this.loginForm)
          if (res.meta.status !== 200) {
            return this.$message.error('用户名或密码错误')
          } else {
            this.$message.success('欢迎您   ' + res.data.username)
            window.sessionStorage.setItem('token', res.data.token)
            this.$router.push('/home')
          }
        } else {
          this.$message.error('请输入所有字段')
          return false
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.login_container {
  height: 100%;
  background-color: #2b4b6b;
}
.login_box {
  width: 500px;
  height: 350px;
  background-color: #fff;
  border-radius: 15px;
  box-shadow: 0 0 25px #cac6c6;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}

.login_label {
  margin-left: 50px;
}

.text {
  text-align: center;
}
.loginTitle {
  text-align: center;
}

.loginRemeber {
  margin: 0px 0px 10px 25px;
}
.loginLpsd {
  width: 100%;
  margin-left: 160px;
}
.login_btn {
  width: 80%;
  text-align: center;
}
.login_ico {
  margin-left: 5px;
}
a {
  font-size: 14px;
}
</style>
